﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

using SynergyControls;
using SynergyLib.DSP;
using SharpDX.XAudio2;
namespace XAudio2Controls
{
    public class LimiterPanel : EffectsPanel, IEffectParams
    {
       
        /// <summary>
        /// 
        /// return a value from -1 to 1.  
        /// -1 = compressor
        /// +1 = expander
        /// </summary>
        public float Slope
        {
            get {
                float slope = DialAngle1 / 360;
                if (DialAngle1 < 180)
                {
                    return -slope;
                }
                else
                { return slope; }
                
                }
            
        }
        public float Release
        {
            get { return DialAngle2 / 360; }
        }
        public float Attack
        { get { return DialAngle3 / 360; } }
        public float Threshhold
        {
            get { return DialAngle4 / 360; }
        }
        public LimiterEffect limiter { get; set; }

         static LimiterPanel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(LimiterPanel), new FrameworkPropertyMetadata(typeof(EffectsPanel)));
        }

         public LimiterPanel(SubmixVoice submixer, Collection<EffectDescriptor> fxChainReference)
        {
            FxChain = fxChainReference;
            EffectParams = this;
            effectType = EffectTypes.Limiter;
            Submixer = submixer;            
            this.Effect = new LimiterEffect();
            //this.Effect = new DelayVibratoEffect();
            limiter = (LimiterEffect)Effect;
            this.effectDescriptor = new EffectDescriptor(Effect);
        }

        protected override void UpdateParameters()
        {
            if (ParameterEnabled)
            {

                LimiterParams prms = new LimiterParams()
                {
                    Slope = this.Param1,
                    Release = this.Param2,
                    Attack = this.Param3,
                    Threshhold = this.Param4
                    
                };
                limiter.Parameters = prms;
                
            }
        }
        protected override void UpdateIntensity()
        {
            //trem.Intensity = (float)IntensitySliderValue;
        }

        #region IEffectParams
        public float Param1
        {
            get
            {
                ParamLabel1Content = GetParam1Readout();
                return Slope;
            }
        }
        public float Param2
        {
            get
            {
                ParamLabel2Content = GetParam2Readout();
                return Release;
            }
        }
        public float Param3
        {
            get
            {
                ParamLabel3Content = GetParam3Readout();
                return Attack;
            }
        }
        public float Param4
        {
            get
            {
                ParamLabel4Content = GetParam4Readout();
                return Threshhold;
            }
        }

        public string ParamName1
        {
            get
            {
                return "Slope";
            }
        }
        public string ParamName2
        {
            get
            {
                return "Release";
            }
        }
        public string ParamName3
        {
            get
            {
                return "Attack";
            }
        }
        public string ParamName4
        {
            get
            {
                return "Threshhold";
            }
        }

        public string GetParam1Readout()
        {
            string sign;
            if (Slope >= 0)
            {
                sign = "+";
            }
            else
            {
                sign = "-";
            }

            return sign + Math.Round(Slope, 2).ToString();
        }
        public string GetParam2Readout()
        {
            return Math.Round(Release, 2).ToString() + "%";
        }
        public string GetParam3Readout()
        {
            return Math.Round(Attack, 2).ToString() + "%";
        }
        public string GetParam4Readout()
        {
            return Math.Round(Threshhold, 2).ToString() + "%";
        }
        #endregion IEffectParams
    }
}
